<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
// 把excel文件中的日期格式的内容转回成标准时间
import { formatExcelDate } from '@/utils/index'
import { importEmployee } from '@/api/employees'
export default {
  name: 'Import',
  data() {
    return {
      list: []
    }
  },
  methods: {
    handleSuccess({ header, results }) {
      console.log(header, results)
      // 时间与属性名格式化为需求名
      const res = this.transExcel(results)
      // 批量导入
      importEmployee(res)
      // 批量导入成功
      this.$message.success('批量导入成功')
      // 返回员工管理页面
      this.$router.brack()
    },
    // 格式化标题
    transExcel(results) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map(item => {
        const obj = {}
        const keys = Object.keys(item)
        console.log(keys)
        keys.map(name => {
          if (mapInfo[name] === 'timeOfEntry' || mapInfo[name] === 'correctionTime') {
            obj[mapInfo[name]] = new Date(formatExcelDate(item[name]))
          } else {
            obj[mapInfo[name]] = item[name]
          }
        })
        return obj
      })
    }
  }
}
</script>
